<template>
  <common-edit-dialog
    :title="dialogTitle"
    :visible.sync="dialogShow"
    @cancel="close"
    @submit="submitForm"
    :button-loading="isPersistenceLoading"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="100px" @submit.native.prevent>
      <el-form-item label="类型编码" prop="dictId">
        <common-input
          v-model="form.dictId"
          placeholder="请输入数据编码"
          disabled
        />
      </el-form-item>
      <el-form-item label="类型名称" prop="dictName">
        <common-input
          v-model="form.dictName"
          placeholder="请输入数据名称"
          disabled
        />
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <common-input
          type="textarea"
          v-model="form.remark"
          placeholder="请输入备注"
        />
      </el-form-item>
    </el-form>
  </common-edit-dialog>
</template>

<script>
import { dict_type_edit } from "@/api/management/configure/index.js";

export default {
  components: {},
  props: {},
  data() {
    return {isPersistenceLoading: false,
      dialogTitle: "编辑", // 标题
      dialogShow: false, // 是否显示
      form: {},
      rules: {
        dictId: [
          {
            required: true,
            message: "请输入数据编码",
            trigger: "blur",
          },
        ],
        dictName: [
          {
            required: true,
            message: "请输入数据名称",
            trigger: "blur",
          },
        ],
      },
    };
  },
  watch: {},
  computed: {},
  methods: {
    /**
     * 关闭
     */
    close() {
      this.$refs.form.resetFields();
      this.dialogShow = false;
      this.$emit("refresh");
    },
    /**
     * 显示
     */
    show(data) {
      this.dialogTitle = data.dictId ? "编辑" : "新增";
      this.dialogShow = true;
      this.form = data.dictId ? data : this.$options.data().form;
    },
    /**
     * 提交
     */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.dictId) {
            if (!this.isPersistenceLoading) {
              this.isPersistenceLoading = true;
              dict_type_edit(this.form).then((res) => {
                if (res && res.code === 200) {
                  this.$GlobalSuccessMessage('操作成功')
                  this.close()
                } else {
                  this.$GlobalErrorConfirm('编辑失败', res.msg)
                }
              }).finally(()=>{
                this.isPersistenceLoading = false
              })
            }
          }
        }
      });
    },
  },
  created() {},
  mounted() {},
};
</script>
<style lang="scss" scoped>
</style>
